package com.encryption.controls.datagrid.render
{
	import com.encryption.controls.datagrid.event.CheckBoxRendererEvent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.text.TextField;
	
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridListData;
	import mx.controls.listClasses.BaseListData;
	
	public class GridCheckBoxRender extends CheckBox
	{
		private var _data:Object;
		private var dataField:String = null;
		
		public function GridCheckBoxRender()
		{
			super();
			this.addEventListener(Event.CHANGE, checkValueChange);
		}
		
		private function checkValueChange(evt:Event){
			if(_data && dataField){
				_data[dataField] = this.selected;
				(this.owner as DataGrid).dispatchEvent(new CheckBoxRendererEvent(CheckBoxRendererEvent.CHECKBOX_CHANGE, _data));
			}
		}
		
		override public function set data(value:Object):void{
			_data = value;
			if(_data && dataField){
				this.selected = _data[dataField];
			}
		}
		
		override public function get data():Object{
			return this._data;
		}
		
		override public function set listData(value:BaseListData):void{
			super.listData = value;
			if(listData is DataGridListData){
				dataField = DataGridListData(listData).dataField;
			}
		}
		
		// 居中展现    
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void    
		{    
			super.updateDisplayList(unscaledWidth, unscaledHeight);    
			var n:int = numChildren;    
			for(var i:int = 0; i < n; i++)    
			{    
				var c:DisplayObject = getChildAt(i);    
				if( !(c is TextField))    
				{    
					c.x = Math.round((unscaledWidth - c.width) / 2);    
					c.y = Math.round((unscaledHeight - c.height) /2 );    
				}    
			}    
		}
	}
}